# -*- coding: UTF-8 -*-

import requests
import json
from __builtin__ import True
import time
#优酷登录
session=requests.session()
session.proxies={"127.0.0.1:8888"}
headers={
         "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36",
         "Accept-Language": "zh-CN,zh;q=0.8",
         "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
         "Accept-Encoding": "gzip,deflate,sdch"
         }

#校验账号的合法性
def check_account(username):    
    r=session.get("http://login.youku.com/user/check?passport="+ username)
    obj=json.loads(r.text);
    print obj
    exists=obj.get("data").get("exists");
    need=obj.get("data").get("need_captcha")
    if exists==1 and need==0:
        return True
    return False
    
##登录帐号
def login(username, password):
    if check_account(username) == True:
        data={
              "passport": username,
              "password": password,
              "captcha":"",
              "remember":"1",
              "callback":"logincallback_"+str(int(time.time()*1000)),
              "from":"http%3A%2F%2Flogin.youku.com%2Fuser%2Flogin%2F%3Fcburl%3Dhttps%253A%252F%252Fwww.google.com.sg%252F@@https%3A%2F%2Fwww.google.com.sg%2F@@",
              "wintype":"page"
              }
        r=session.post("https://login.youku.com/user/login_submit/", data=data, verify=False, headers=headers)
        print r.text


login("15057126798", "yanghui")

        
